<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Good;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class GoodController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */

    protected $name = "商品列表";
    protected function grid()
    {
        return Grid::make(new Good(), function (Grid $grid) {
            $grid->model()->orderBy('sort', 'desc');
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('cover')->image(width:50);
            $grid->column('sale','已兑换')->sortable();
            $grid->column('stock','库存')->sortable();

            $grid->column('desc');
            $grid->column('status')->switch("1","0");
            $grid->column('sort')->editable();
            $grid->column('integral');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();


            $grid->disableViewButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('name');
                $filter->equal('status')->select([1 => '启用', 0 => '禁用']);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Good(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('cover');
            $show->field('images');
            $show->field('desc');
            $show->field('status');
            $show->field('sort');
            $show->field('content');
            $show->field('integral');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Good(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->image('cover')->autoUpload()->uniqueName();
            $form->number('integral');
            $form->multipleFile('images')->autoUpload();
            $form->textarea('desc');
            $form->switch('status')->default(1);
            $form->number('sort')->default(1);
            $form->editor('content');

            $form->disableViewButton();
        });
    }
}
